
package com.sun.showcase.biz.basic.service.impl;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.basic.dao.GroupUserDao;
import com.sun.showcase.biz.basic.service.GroupUserService;
import com.sun.showcase.client.domain.basic.GroupUser;
import com.sun.showcase.client.query.basic.GroupUserQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
@Service("groupUserService")
@Transactional
public class GroupUserServiceImpl implements GroupUserService{
	@Autowired
	private GroupUserDao groupUserDao;
	
	public void setGroupUserDao(GroupUserDao dao) {
		this.groupUserDao = dao;
	}

	public DataGrid datagrid(GroupUserQuery groupUserQuery) {
		DataGrid j = new DataGrid();
		Pager<GroupUser> pager  = find(groupUserQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<GroupUserQuery> getQuerysFromEntitys(List<GroupUser> groupUsers) {
		List<GroupUserQuery> groupUserQuerys = new ArrayList<GroupUserQuery>();
		if (groupUsers != null && groupUsers.size() > 0) {
			for (GroupUser tb : groupUsers) {
				GroupUserQuery b = new GroupUserQuery();
				BeanUtils.copyProperties(tb, b);
				groupUserQuerys.add(b);
			}
		}
		return groupUserQuerys;
	}

	private Pager<GroupUser> find(GroupUserQuery groupUserQuery) {
		groupUserQuery.setOrderColum("GROUP_ID");
		Page<GroupUser> page = PageHelper.startPage(groupUserQuery.getPage().intValue(), groupUserQuery.getRows().intValue());
		List<GroupUser> list = groupUserDao.findPage(groupUserQuery);
		Pager<GroupUser> pager = new Pager<GroupUser>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public GroupUser add(GroupUserQuery groupUserQuery) {
		GroupUser t = new GroupUser();
		BeanUtils.copyProperties(groupUserQuery, t);
		groupUserDao.save(t);
		groupUserQuery.setUserId(t.getUserId());
		return t;
		
	}

	public void update(GroupUserQuery groupUserQuery) {
		GroupUser t = groupUserDao.get(groupUserQuery);
	    if(t != null) {
	    	BeanUtils.copyProperties(groupUserQuery, t);
	    	groupUserDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(GroupUserQuery groupUserQuery) {
	    groupUserDao.updatePart(groupUserQuery);
	}

	public void delete(java.lang.String[] ids) {
		groupUserDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.String[] ids) {
		groupUserDao.deletePt(ids);
	}

	public GroupUser get(GroupUserQuery groupUserQuery) {
		return groupUserDao.getById(groupUserQuery.getUserId());
	}

	public GroupUser get(String id) {
		return groupUserDao.getById(id);
	}

	
	public List<GroupUserQuery> listAll(GroupUserQuery groupUserQuery) {
		groupUserQuery.setOrderColum("GROUP_ID");
	    List<GroupUser> list = groupUserDao.findList(groupUserQuery);
		List<GroupUserQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	public void delete(GroupUserQuery groupUserQuery) {
		if (groupUserQuery != null) {
			groupUserDao.delete(groupUserQuery);
		}
	}

	@Override
	public GroupUser getByUserAndGroup(GroupUserQuery groupUserQuery) {
	
		return groupUserDao.get(groupUserQuery);
	}
	
}
